/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.iecg.controller.app;

import com.iecg.api.dto.IecgNoticeDto;
import com.iecg.api.vo.IecgNoticeVO;
import com.iecg.service.IecgUserService;
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 用户紧急联系
 *
 * @author pigx code generator
 * @date 2019-10-16 09:58:43
 */
@RestController
@AllArgsConstructor
@RequestMapping("app/notice" )
@Api(value = "app_notice", tags = "App消息通知")
public class AppNoticeController {

	private final IecgUserService iecgUserService;

	private final RedisTemplate redisTemplate;

	/**
	 * 获取消息通知
	 * @param
	 * @return R
	 */
	@ApiOperation(value = "获取消息通知", notes = "获取消息通知")
	@SysLog("获取消息通知" )
	@GetMapping
	public R get() {
		Long userId = iecgUserService.getSecurityUser().getId();

		BoundHashOperations<String, String, IecgNoticeDto> boundHashOperations = redisTemplate.boundHashOps(CacheConstants.IECG_NOTICT + ":" + userId.toString());
		List<IecgNoticeDto> iecgNoticeDtoList =  boundHashOperations.values().stream().filter((iecgNoticeDto)-> {
				Date d1 = new Date();
				Date d2 = iecgNoticeDto.getCretatTime();
				long diff = d1.getTime() - d2.getTime();
				long days = diff / (1000 * 60 * 60 * 24);
				if(days <= 7){
					return true;
				}
		        return false;
		}).sorted(Comparator.comparing(IecgNoticeDto::getCretatTime).reversed()).collect(Collectors.toList());
		return R.ok(iecgNoticeDtoList);
	}


	/**
	 * 修改消息通知状态
	 * @param
	 * @return R
	 */
	@ApiOperation(value = "修改消息通知状态", notes = "修改消息通知状态")
	@SysLog("修改消息通知状态" )
	@PutMapping
	public R updateById(@RequestBody IecgNoticeVO iecgNoticeVO) {
		Long userId = iecgUserService.getSecurityUser().getId();
		String msg = iecgNoticeVO.getMsg();
		BoundHashOperations<String, String, Object> boundHashOperations = redisTemplate.boundHashOps(userId);
		boundHashOperations.put(msg, false);
		return R.ok();
	}
}
